package com.fengqilin.myapp;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    int[] ints = new int[9];
    EditText[] editTexts = new EditText[9];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.calculateButton).setOnClickListener(this);
        findViewById(R.id.historyButton).setOnClickListener(this);
        ints[0] = R.id.editTextCoin01;
        ints[1] = R.id.editTextCoin05;
        ints[2] = R.id.editTextCoin1;
        ints[3] = R.id.editText1;
        ints[4] = R.id.editText5;
        ints[5] = R.id.editText10;
        ints[6] = R.id.editText20;
        ints[7] = R.id.editText50;
        ints[8] = R.id.editText100;

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.historyButton) {
            JSONObject jsonObject = new JSONObject();
            for (int i = 0; i < ints.length; i++) {
                if (editTexts[i] == null)
                    editTexts[i] = (EditText) findViewById(ints[i]);
                String str = editTexts[i].getText().toString();
                try {
                    jsonObject.put(CashierEntity.strings[i], str);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            Log.d("==========data:", jsonObject.toString());
            Intent intent = new Intent(MainActivity.this, SumActivity.class);
            intent.putExtra("data", jsonObject.toString());
            startActivity(intent);
        } else if (v.getId() == R.id.historyButton) {
            startActivity(new Intent(this, HistoryActivity.class));
        }
    }
}